﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using NjamNjamAdminApp.ImageService;
using NjaNjam.MongoRepository;
using NjamNjam.Model.Entities;
using System.Linq;
using NjamNjamAdminApp.Interfaces;

namespace NjamNjamAdminApp.Views
{
    public partial class VrsteRestoranaView : DevExpress.XtraEditors.XtraForm, IView
    {
        readonly MongoRepository m_Repository = new MongoRepository();

        private readonly BindingList<VrstaRestorana> m_VrsteRestorana = new BindingList<VrstaRestorana>(); 

        public VrsteRestoranaView()
        {
            InitializeComponent();
            vrstaRestoranaBindingSource.DataSource = m_VrsteRestorana;
            m_VrsteRestorana.AddingNew +=
                (s, e) =>
                {
                    long id = 1;
                    if (m_VrsteRestorana.Count > 0)
                    {
                        id = m_VrsteRestorana.Max(x => x.Id) + 1;
                    }
                    e.NewObject = new VrstaRestorana { Id = id, Ikona = Guid.NewGuid().ToString()};
                };
        }

        public void NoviUnos()
        {
            m_VrsteRestorana.AddNew();
            vrstaRestoranaBindingSource.MoveLast();
        }

        void IView.Sacuvaj()
        {
            Sacuvaj();
        }

        public void Obrisi()
        {
            var vrstaRestorana = vrstaRestoranaBindingSource.Current as VrstaRestorana;
            if (vrstaRestorana != null)
            {
                m_Repository.Obrisi(vrstaRestorana);
                m_VrsteRestorana.Remove(vrstaRestorana);
            }
        }

        void IView.Osvezi()
        {
            Osvezi();
        }

        public void Izvezi()
        {
            using (var svc = new ImageServiceClient("Http"))
            {
                try
                {
                    foreach (var vrstaRestorana in m_VrsteRestorana)
                    {
                        var response = svc.SacuvajIkonuVrsteRestorana(new SaveImageRequest { Id = vrstaRestorana.Id, Image = vrstaRestorana.Slika });
                        if (response.ErrorMessage != null)
                        {
                            ((IShell)MdiParent).ShowWarning("Nesupešan izvoz slike " + vrstaRestorana.Naziv + ": " + response.ErrorMessage);
                            break;
                        }
                    }
                }
                catch (Exception exc)
                {
                    ((IShell)MdiParent).ShowWarning("Nesupešan izvoz slike: " + exc.Message);
                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            Osvezi();
        }

        private void Sacuvaj()
        {
            foreach (VrstaRestorana vrstaRestorana in m_VrsteRestorana)
            {
                //if (vrstaRestorana.Ikona != null && vrstaRestorana.Slika != null)
                //{
                //    Utils.Image.SaveImage(vrstaRestorana.Ikona, vrstaRestorana.Slika);
                //}
                m_Repository.Sacuvaj(vrstaRestorana);
            }
        }

        private void Osvezi()
        {
            m_VrsteRestorana.Clear();
            foreach (var item in m_Repository.VrsteRestorana())
            {
                m_VrsteRestorana.Add(item);
            }
        }
    }
}